using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class controls_usrCarDriver : System.Web.UI.UserControl
{
    protected Int64 lngDriverId;
    protected string strDriverNo;
    protected string strSurName;
    protected string strGivenName;
    protected string strSal;
    protected string strMaritalStatus;
    protected DateTime dtDOB;
    protected char chrGender;
    protected string strIDType;
    protected string strIDNo;
    protected string strOccupation;
    protected string strJobNature;
    protected byte byteYearOfDriving;
    protected string strValidationGroup;

    protected bool bEnabled = true;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            usrDOB.StartYear = CE.Global.General.GetUTCDateTime().Year - 60;
            usrDOB.EndYear = CE.Global.General.GetUTCDateTime().Year - 18;
            txtSurName.Attributes.Add("onblur", "ShowValidationEffect('" + txtSurName.ClientID + "','" + rfvSurName.ClientID + "');");
            ddlYersOfDriving.Attributes.Add("onblur", "ShowValidationEffect('" + ddlYersOfDriving.ClientID + "','" + rfvDrivingYearSg.ClientID + "');");
            ddlIdType.ClientValidationScript = "CheckDriver" + strDriverNo + "IdTypePrefix();";
            txtIDNo.ClientValidationScript = "CheckDriver" + strDriverNo + "IdTypePrefix();";

            //ClearProperty();
        }
    }

    public void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvSurName.Enabled && rfvSurName.ValidationGroup == strValidationGroup)
        {
            rfvSurName.Validate();
            if (rfvSurName.IsValid == false)
            {
                txtSurName.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtSurName.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtSurName.ControlStyle.CssClass = "";
        }

        ddlSalutation.ChangeValidationEffectServerSide(strValidationGroup);
        ddlGender.ChangeValidationEffectServerSide(strValidationGroup);
        ddlIdType.ChangeValidationEffectServerSide(strValidationGroup);
        ddlOccupation.ChangeValidationEffectServerSide(strValidationGroup);
        ddlJobNature.ChangeValidationEffectServerSide(strValidationGroup);
        ddlMaritalStatus.ChangeValidationEffectServerSide(strValidationGroup);
        txtIDNo.ChangeValidationEffectServerSide(strValidationGroup);
        usrDOB.ChangeValidationEffectServerSide(strValidationGroup);

        if (rfvDrivingYearSg.Enabled && rfvDrivingYearSg.ValidationGroup == strValidationGroup)
        {
            rfvDrivingYearSg.Validate();
            if (rfvDrivingYearSg.IsValid == false)
            {
                ddlYersOfDriving.ControlStyle.CssClass = "validationError";
            }
            else
            {
                ddlYersOfDriving.ControlStyle.CssClass = "";
            }
        }
        else
        {
            ddlYersOfDriving.ControlStyle.CssClass = "";
        }
    }

    public bool IsEnabled
    {
        get { return bEnabled; }
        set
        {
            bEnabled = value;
            SetEnabled();
        }
    }

    public string ValidationGroup
    {
        get { return strValidationGroup; }
        set { strValidationGroup = value; }
    }

    public string DriverNo
    {
        get
        {
            return strDriverNo;
        }
        set
        {
            strDriverNo = value;
        }
    }

    public Int64 DriverId
    {
        get
        {
            return lngDriverId;
        }
        set
        {
            lngDriverId = value;
        }
    }

    public string SurName
    {
        get
        {
            return strSurName;
        }
        set
        {
            strSurName = value;
        }
    }

    public string GivenName
    {
        get
        {
            return strGivenName;
        }
        set
        {
            strGivenName = value;
        }
    }

    public string Salutation
    {
        get
        {
            return strSal;
        }
        set
        {
            strSal = value;
        }
    }

    public string MaritalStatus
    {
        get
        {
            return strMaritalStatus;
        }
        set
        {
            strMaritalStatus = value;
        }
    }

    public DateTime DOB
    {
        get
        {
            return dtDOB;
        }
        set
        {
            dtDOB = value;
        }
    }

    public char Gender
    {
        get
        {
            return chrGender;
        }
        set
        {
            chrGender = value;
        }
    }
    
    public string IDType
    {
        get
        {
            return strIDType;
        }
        set
        {
            strIDType = value;
        }
    }

    public string IDNo
    {
        get
        {
            return strIDNo;
        }
        set
        {
            strIDNo = value;
        }
    }

    public string Occupation
    {
        get
        {
            return strOccupation;
        }
        set
        {
            strOccupation = value;
        }
    }

    public string JobNature
    {
        get
        {
            return strJobNature;
        }
        set
        {
            strJobNature = value;
        }
    }

    public byte YearsOfDriving
    {
        get
        {
            return byteYearOfDriving;
        }
        set
        {
            byteYearOfDriving = value;
        }
    }

    public void SetEnabled()
    {
        ddlGender.IsEnabled = bEnabled;
        ddlSalutation.IsEnabled = bEnabled;
        ddlIdType.IsEnabled = bEnabled;
        ddlOccupation.IsEnabled = bEnabled;
        ddlJobNature.IsEnabled = bEnabled;
        ddlYersOfDriving.Enabled = bEnabled;
        ddlMaritalStatus.IsEnabled = bEnabled;
        txtSurName.Enabled = bEnabled;
        txtGivenName.Enabled = bEnabled;
        txtIDNo.IsEnabled = bEnabled;
        usrDOB.IsEnabled = bEnabled;
        rfvSurName.Enabled = bEnabled;
        rfvDrivingYearSg.Enabled = bEnabled;
    }

    public void IsEnableValidation(bool bValue)
    {
        ddlGender.IsEnableValidation(bValue);
        ddlSalutation.IsEnableValidation(bValue);
        ddlIdType.IsEnableValidation(bValue);
        ddlOccupation.IsEnableValidation(bValue);
        ddlJobNature.IsEnableValidation(bValue);
        rfvDrivingYearSg.Enabled = bValue;
        ddlMaritalStatus.IsEnableValidation(bValue);
        rfvSurName.Enabled = bValue;
        txtIDNo.IsEnableValidation(bValue);
        usrDOB.IsEnableValidation(bValue);
    }

    protected override void OnPreRender(EventArgs e)
    {
        SetEnabled();

        if (strDriverNo != null)
        {
            lblDriverNo.Text = "Driver " + strDriverNo;
        }

        if (strValidationGroup != null)
        {
            ddlSalutation.ValidationGroup = strValidationGroup;
            ddlIdType.ValidationGroup = strValidationGroup;
            ddlGender.ValidationGroup = strValidationGroup;
            ddlMaritalStatus.ValidationGroup = strValidationGroup;
            ddlOccupation.ValidationGroup = strValidationGroup;
            ddlJobNature.ValidationGroup = strValidationGroup;
            rfvSurName.ValidationGroup = strValidationGroup;
            rfvDrivingYearSg.ValidationGroup = strValidationGroup;
            usrDOB.ValidationGroup = strValidationGroup;
            txtIDNo.ValidationGroup = strValidationGroup;

            //ddlSalutation.ErrorMessage = "Choose " + lblDriverNo.Text + "'s salutation.";

            string scriptSal = "function GetDriver" + strDriverNo + "Salutation()";
            scriptSal += "{";
            scriptSal += "return document.getElementById('" + ddlSalutation.ClientID + "_ddlSal').value";
            scriptSal += "}";
            Page.ClientScript.RegisterClientScriptBlock(ddlSalutation.GetType(), ddlSalutation.ID.ToString() + strDriverNo, scriptSal, true);

            //ddlIdType.ErrorMessage = "Choose " + lblDriverNo.Text + "'s ID type.";
            //usrDOB.ErrorMessage = "Choose " + lblDriverNo.Text + "'s date of birth.";
            //txtIDNo.PrefixErrorMessage = "Choose " + lblDriverNo.Text + "'s ID number initial character.";
            //txtIDNo.PostfixErrorMessage = "Choose " + lblDriverNo.Text + "'s ID number last character.";
            //txtIDNo.IdNoErrorMessage = lblDriverNo.Text + "'s ID Number is not valid.";
            //txtIDNo.IdNoLengthErrorMessage = lblDriverNo.Text + "'s ID number must be 7 digits.";
            //txtIDNo.IdNoRequireErrorMessage = "Require " + lblDriverNo.Text + "'s ID number.";

            string scriptIDNumber = "function GetDriver" + strDriverNo + "IDNumber()";
            scriptIDNumber += "{";
            scriptIDNumber += "var IdNumber = '';";
            scriptIDNumber += "IdNumber = document.getElementById('" + txtIDNo.ClientID + "_ddlIdPrefix').value;";
            scriptIDNumber += "IdNumber += document.getElementById('" + txtIDNo.ClientID + "_txtIdNumber').value;";
            scriptIDNumber += "IdNumber += document.getElementById('" + txtIDNo.ClientID + "_ddlIdPostfix').value;";
            scriptIDNumber += "return IdNumber;";
            scriptIDNumber += "}";
            Page.ClientScript.RegisterClientScriptBlock(txtIDNo.GetType(), txtIDNo.ID.ToString() + strDriverNo, scriptIDNumber, true);

            ddlGender.ErrorMessage = "Choose " + lblDriverNo.Text + "'s gender.";
            string scriptGender = "function GetDriver" + strDriverNo + "Gender()";
            scriptGender += "{";
            scriptGender += "return document.getElementById('" + ddlGender.ClientID + "_ddlGender').value";
            scriptGender += "}";
            Page.ClientScript.RegisterClientScriptBlock(ddlGender.GetType(), ddlGender.ID.ToString() + strDriverNo, scriptGender, true);

            //ddlOccupation.ErrorMessage = "Choose " + lblDriverNo.Text + "'s Occupation.";
            //ddlJobNature.ErrorMessage = "Choose " + lblDriverNo.Text + "'s job nature.";
            //ddlMaritalStatus.ErrorMessage = "Choose " + lblDriverNo.Text + "'s marital status.";
            //rfvSurName.ErrorMessage = "Require " + lblDriverNo.Text + "'s surname.";
            //rfvDrivingYearSg.ErrorMessage = "Choose " + lblDriverNo.Text + "'s years of driving in singapore.";

            string scriptPrefixId = "function GetDriver" + strDriverNo + "PrefixID()";
            scriptPrefixId += "{";
            scriptPrefixId += "var prefixId = '" + txtIDNo.ClientID + "_ddlIdPrefix';";
            scriptPrefixId += "return prefixId;";
            scriptPrefixId += "}";
            Page.ClientScript.RegisterClientScriptBlock(txtIDNo.GetType(), txtIDNo.ID.ToString() + "2" + strDriverNo, scriptPrefixId, true);

            string scriptIdType = "function GetDriver" + strDriverNo + "IdTypeID()";
            scriptIdType += "{";
            scriptIdType += "var IdTypeId = '" + ddlIdType.ClientID + "_ddlIdType';";
            scriptIdType += "return IdTypeId;";
            scriptIdType += "}";
            Page.ClientScript.RegisterClientScriptBlock(ddlIdType.GetType(), ddlIdType.ID.ToString() + strDriverNo, scriptIdType, true);
        }
        
        if (lngDriverId != 0)
        {
            lblDriverId.Text = lngDriverId.ToString();
        }

        if (strSal != null)
        {
            ddlSalutation.Saluation = strSal;
        }

        if (strSurName != null)
        {
            txtSurName.Text = strSurName;
        }

        if (strGivenName != null)
        {
            txtGivenName.Text = strGivenName;
        }

        if (chrGender.ToString() != string.Empty)
        {
            if (chrGender == '0')
            {
                ddlGender.Gender = "Select";
            }
            else
            {
                ddlGender.Gender = chrGender.ToString();
            }
        }

        if (strMaritalStatus != null)
        {
            ddlMaritalStatus.MaritalStatus = strMaritalStatus;
        }

        if (strIDType != null)
        {
            ddlIdType.IdType = strIDType;
        }

        if (strIDNo != null && strIDNo.Length == 9)
        {
            strIDNo = strIDNo.Substring(0, 9);
            txtIDNo.Prefix = strIDNo.Substring(0, 1);
            txtIDNo.IDNumber = strIDNo.Substring(1, 7);
            txtIDNo.Postfix = strIDNo.Substring(8, 1);
        }

        if (strOccupation != null)
        {
            ddlOccupation.Occupation = strOccupation;
        }

        if (strOccupation != null)
        {
            ddlJobNature.JobNature = strJobNature;
        }

        if (byteYearOfDriving != 0)
        {
            ddlYersOfDriving.Text = byteYearOfDriving.ToString();
        }

        if (dtDOB != null)
        {
            //dtDOB = CE.Global.General.GetUTCDateTime().Date;
            usrDOB.Day = dtDOB.Day;
            usrDOB.Month = dtDOB.Month;
            usrDOB.Year = dtDOB.Year;
        }
    }

    public void SetProperty()
    {
        if (this.Page.IsValid)
        {
            lngDriverId = Convert.ToInt64(lblDriverId.Text == string.Empty ? "0" : lblDriverId.Text);
            strSal = ddlSalutation.GetSalutation();
            strSurName = txtSurName.Text;
            strGivenName = txtGivenName.Text;
            chrGender = Convert.ToChar(ddlGender.GetGender());
            strMaritalStatus = ddlMaritalStatus.GetMaritalStatus();
            dtDOB = usrDOB.GetDate();

            strIDType = ddlIdType.GetIDType();
            strIDNo = txtIDNo.GetIdNumber();

            strOccupation = ddlOccupation.GetOccupation();
            strJobNature = ddlJobNature.GetJobNature();
            byteYearOfDriving = Convert.ToByte(ddlYersOfDriving.SelectedValue.ToString());
        }
    }

    public void ClearProperty()
    {
        lngDriverId = 0;
        strSal = "";
        strSurName = "";
        strGivenName = "";
        chrGender = '0';
        strMaritalStatus = "";
        dtDOB = CE.Global.General.GetUTCDateTime().Date;

        strIDType = "Select";
        strIDNo = "";

        strOccupation = "";
        strJobNature = "";
        byteYearOfDriving = 0;
    }
}
